<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class InitController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
        //$this->_helper->viewRenderer->setNoRender();    //ko dung View
        $this->_helper->layout->disableLayout();        //ko load Layout vào ứng dụng 
    }
    
    /*** Test Data Action 
     * Zend_Config_Ini
     * Zend_Config_Xml
     * Zend_Config_Writer_Xml
     * **/
    

    public function  indexAction()
    {
        /*     
        //thông tin cấu hình
        $configArray = array(
          "webhost"  => "www.example.com",
          "database" => array(
              "adapter" => "pdo_mysql",
              "params"  => array(
                  "host"     => "localhost",
                  "username" => "root",
                  "password" => "",
                  "dbname"   => "db_mvc"
                    )
             )
        );
     
        $config = new Zend_Config($configArray, true);
        
        echo "<br>".$config->webhost;//output:"www.example.com     
     
        echo"<br>". $config->database->adapter;//output:pdo_mysql
        
        $db = Zend_Db::factory($config->database);
        */        
        
        $filename= CONFIG_PATH ."/application.ini";     
        $section = "production";
        $options = array("allowModifications"=>true,"nestSeparator"=>".");
        
        $config = new Zend_Config_Ini($filename, $section, $options);
        
        //$config->resources->db->params->dbname = "test";
        
        echo "<pre>";
        print_r($config->resources->db->toArray());
        echo "</pre>";
    }


    public function xmlAction(){
      $xml = CONFIG_PATH."/config.xml";
      $section = "production";
      $options = true;
      $config = new Zend_Config_Xml($xml,$section,$options);
      echo $config->database->params->host;
      $config->database->params->dbname = "test";
      echo "<br>".$config->database->params->dbname;
               echo "<pre>";
      print_r($config->toArray());
      echo "</pre>";
   }
   
   public function menuAction() {
        $xml = CONFIG_PATH. "/menu.xml";        
        $config = new Zend_Config_Xml($xml, "nav");
        
        $container = new Zend_Navigation($config);
        $this->view->container = $container;                      
    }
   
    public function writeXmlAction(){
        //tạo 1 đối tượng $config rỗng sau 
        $config = new Zend_Config(array(), true);
        $config->production = array();
        $config->staging    = array();        
        
        $config->setExtend("staging", "production");
        
        $config->production->db = array();
        $config->production->db->hostname = "localhost";
        $config->production->db->username = "production";
        
        $config->staging->db = array();
        $config->staging->db->username = "staging";
        
        // Write the config file in one of the following ways:
        // a)
        $filename = CONFIG_PATH."\writeXml.xml";
        $writer = new Zend_Config_Writer_Xml(array("config"   => $config,
                                                 "filename" => $filename));
        $writer->write();
        
        $writer = new Zend_Config_Writer_Xml();
        $filename = $filename = CONFIG_PATH."\writeXml2.xml";
        $writer->setConfig($config)
        ->setFilename($filename)
        ->write();
        
        $filename = CONFIG_PATH."\writeXml3.xml";
        $writer = new Zend_Config_Writer_Xml();
        $writer->write($filename, $config);
    }
    
    public function modifyFileAction(){
        $xml= CONFIG_PATH . "\writeXml2.xml";
        $section = null;
        $options=array("skipExtends"        => true,
                       "allowModifications" => true);
        $config = new Zend_Config_Xml($xml,$section,$options);
        echo "<pre>";
        print_r($config->toArray());
        echo "</pre>";
        $config->production->db->hostname = "foobar";
        $writer = new Zend_Config_Writer_Xml(array("config"   => $config,
                                           "filename" => $xml));
        $writer->write();
    }
    
    
}
?>
